package xyz.yuzegod.megawalls.game;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.plugin.Plugin;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.event.GameOverEvent;
import xyz.yuzegod.megawalls.event.GameStartEvent;
import xyz.yuzegod.megawalls.timer.GameOverTimer;

import java.util.UUID;

public class GameTimer implements Runnable
{
    private int time;
    private int waitSecond;
    private boolean flag;
    
    public GameTimer() {
        this.time = 360;
        this.waitSecond = 0;
        this.flag = false;
    }
    
    @Override
    public void run() {
        if (this.time == -1) {
            return;
        }
        --this.time;
        final Game game = MegaWalls.getInstance().getGame();
        if (game.isWaiting()) {
            if (Bukkit.getOnlinePlayers().size() < game.getMinPlayers()) {
                this.flag = false;
                this.time = 360;
                for (final Player p : Bukkit.getOnlinePlayers()) {
                    p.setExp(0.0f);
                    p.setLevel(0);
                }
                if (this.waitSecond >= 360) {
                    this.waitSecond = 0;
                }
                else {
                    ++this.waitSecond;
                }
            }
            else {
                if (!this.flag) {
                    this.time = 120;
                    this.flag = true;
                }
                for (final Player p : Bukkit.getOnlinePlayers()) {
                    p.setExp(0.0f);
                    p.setLevel((this.time > 120) ? 0 : this.time);
                }
            }
            for (final GamePlayer gamePlayer : GamePlayer.getOnlinePlayers()) {
                gamePlayer.sendActionBar("§c使用§b/mparty invite 玩家名§c进行组队！");
            }
        }
        else if (game.isStarted()) {
            for (final Player p : Bukkit.getOnlinePlayers()) {
                for (final Player player : Bukkit.getOnlinePlayers()) {
                    final int health = (int)player.getHealth();
                    Objective obj = p.getScoreboard().getObjective(DisplaySlot.BELOW_NAME);
                    if (obj == null) {
                        obj = p.getScoreboard().registerNewObjective(String.valueOf(UUID.randomUUID().toString().substring(0, 6)), "health");
                        obj.setDisplaySlot(DisplaySlot.BELOW_NAME);
                    }
                    obj.setDisplayName(ChatColor.RED + "❤");
                    obj.getScore((OfflinePlayer)player).setScore(health);
                    obj = p.getScoreboard().getObjective(DisplaySlot.PLAYER_LIST);
                    if (obj == null) {
                        obj = p.getScoreboard().registerNewObjective(String.valueOf(UUID.randomUUID().toString().substring(0, 6)), "tabhealth");
                        obj.setDisplaySlot(DisplaySlot.PLAYER_LIST);
                    }
                    obj.getScore((OfflinePlayer)player).setScore(health);
                }
            }
            if (this.time % 300 == 0) {
                game.broadcastMessage("§c§l如果你断开连接， 可输入/rejoin重新加入游戏。");
            }
            for (GameTeam gameTeam:game.getTeams()){
                if (!gameTeam.isWitherDead()){
                    if (this.time%8==0){
                        gameTeam.getTeamWither().setHealth(gameTeam.getTeamWither().getHealth()-5);
                        if (gameTeam.getTeamWither().getHealth()<=0){
                            gameTeam.getTeamWither().die();
                        }
                    }
                }
            }
            if (game.isOver() && game.getGameOverTimer() == null) {
                Bukkit.getPluginManager().callEvent((Event)new GameOverEvent(game));
                Bukkit.getScheduler().runTaskTimer((Plugin)MegaWalls.getInstance(), (Runnable)new GameOverTimer(game), 0L, 20L);
            }
        }
        if (this.time == 0) {
            this.time = -1;
            this.finishCountdown();
        }
        else {
            this.broadcastState(this.time);
        }
    }
    
    public void broadcastState(final int time) {
        final Game game = MegaWalls.getInstance().getGame();
        switch (game.getState()) {
            case LOBBY: {
                if (Bukkit.getOnlinePlayers().size() < game.getMinPlayers()) {
                    final int diff = game.getMinPlayers() - Bukkit.getOnlinePlayers().size();
                    if (this.waitSecond == 0 && Bukkit.getOnlinePlayers().size() > 0) {
                        game.broadcastMessage(" ");
                        game.broadcastMessage("§f仍需§a" + diff + "§f名玩家加入才可开始游戏。");
                        game.broadcastMessage(" ");
                        this.playSound(Sound.CHICKEN_EGG_POP);
                    }
                    return;
                }
                if (time % 60 == 0 && time != 60) {
                    Bukkit.broadcastMessage("§e游戏将在§c" + time / 60 + "§e分钟后开始！");
                    this.playSound(Sound.CLICK);
                    break;
                }
                if (time == 60 || time == 30 || time == 10 || time == 5 || time == 4 || time == 3 || time == 2 || time == 1) {
                    Bukkit.broadcastMessage("§e游戏将在§c" + time + "§e秒后开始！");
                    game.broadcastTitle("§c§l" + time, "§e准备战斗吧！", 0, 20, 0);
                    this.playSound(Sound.CLICK);
                    break;
                }
                break;
            }
            case INGAME: {
                if (time == 15 || time <= 3) {
                    Bukkit.broadcastMessage("§e游戏将在§c" + time + "§e秒后结束！");
                    this.playSound(Sound.CLICK);
                    break;
                }
                break;
            }
        }
    }
    
    public void finishCountdown() {
        switch (MegaWalls.getInstance().getGame().getState()) {
            case INGAME: {
                Bukkit.getPluginManager().callEvent((Event)new GameOverEvent(MegaWalls.getInstance().getGame()));
                final Game game = MegaWalls.getInstance().getGame();
                if (game.isOver() && game.getGameOverTimer() == null) {
                    Bukkit.getPluginManager().callEvent((Event)new GameOverEvent(game));
                    Bukkit.getScheduler().runTaskTimer((Plugin)MegaWalls.getInstance(), (Runnable)new GameOverTimer(game), 0L, 20L);
                    break;
                }
                break;
            }
            case LOBBY: {
                if (MegaWalls.getIngame().size() >= MegaWalls.getInstance().getGame().getMinPlayers()) {
                    Bukkit.getPluginManager().callEvent((Event)new GameStartEvent(MegaWalls.getInstance().getGame()));
                    MegaWalls.getInstance().getGame().onStart();
                    this.setTime(3925);
                    break;
                }
                this.setTime(60);
                break;
            }
        }
    }
    
    private void playSound(final Sound sound) {
        for (final Player p : Bukkit.getOnlinePlayers()) {
            p.playSound(p.getLocation(), sound, 1.0f, 1.0f);
        }
    }
    
    public void setTime(final int time) {
        this.time = time;
    }
    
    public int getTime() {
        return this.time;
    }
}
